package com.xhlj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhlj.entity.Activity;
import com.xhlj.entity.Registrations;
import com.xhlj.service.ActivityService;
import com.xhlj.mapper.ActivityMapper;
import com.xhlj.service.RegistrationsService;
import com.xhlj.utils.JwtHelper;
import com.xhlj.utils.Result;
import io.jsonwebtoken.Jwt;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
* @author Yzc
* @description 针对表【activity】的数据库操作Service实现
* @createDate 2024-03-20 14:14:18
*/
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity>
    implements ActivityService{

    @Resource
    private RegistrationsService registrationsService;

    @Resource
    private JwtHelper jwtHelper;

    //TODO:根据标签获取活动信息
    @Override
    public IPage<Activity> getActivitiesByOpr(Page<Activity> page, String tag) {
        LambdaQueryWrapper<Activity> queryWrapper=new LambdaQueryWrapper<>();
        if(!Objects.equals(tag, "all")) queryWrapper.like(Activity::getActivityTag,tag);
        queryWrapper.orderByDesc(Activity::getStartTime);

        return baseMapper.selectPage(page,queryWrapper);
    }

    //TODO：活动报名
    @Override
    public Result registerActivityById(Integer id,String token) {
        //todo:1.判断该活动是否是可报名的
        if(Objects.equals(token, "")){
            return Result.build(null,400,"请先登录");
        }
        Activity activity = baseMapper.selectById(id);

        //活动不是可报名的
        if(activity.getIsRegistrable()!=1){
            return Result.build(null,405,"该活动不可报名");
        };

        //活动status不为active
        if(activity.getStatus()!="active"){
            return Result.build(null,406,"报名未在有效时间内");
        }

        //todo:2.若可报名，则查找该用户是否已经报名过这个活动

        Integer userId=jwtHelper.getPayLoad(token).getUserId();
        Integer activityId=activity.getActivityId();
        LambdaQueryWrapper<Registrations> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Registrations::getActivityId,activityId)
                    .eq(Registrations::getUserId,userId);
        if(registrationsService.getOne(queryWrapper)!=null){
            return Result.build(null,407,"您已报名该活动");
        }

        //todo:3.将信息加入registrations表中
        Registrations registration=new Registrations();
        registration.setActivityId(activityId);
        registration.setUserId(userId);
        registration.setRegisTime(new Date());
        registrationsService.save(registration);

        //todo:4.返回报名成功信息
        return  Result.ok("报名成功");
    }

    //TODO:查询指定Id的活动数据
    @Override
    public Result getActivityDataByid(Integer id) {
        LambdaQueryWrapper<Registrations> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Registrations::getActivityId,id);
        return Result.ok(registrationsService.count(queryWrapper));
    }

    @Override
    public Result getActivitiesData() {
        List<Activity> activities = baseMapper.selectList(null);
        HashMap<String,Integer> map=new HashMap<>();
        //TODO:遍历所有活动
        for(Activity activity:activities){
            //TODO:如果活动是可报名的，统计他的报名人数
            if (activity.getIsRegistrable()==1){
                LambdaQueryWrapper<Registrations> queryWrapper=new LambdaQueryWrapper<>();
                queryWrapper.eq(Registrations::getActivityId,activity.getActivityId());
                map.put(activity.getActivityName(), (int) registrationsService.count(queryWrapper));
            }
        }
        return Result.ok(map);
    }
}




